package com.telenav.snowsweeper.controller;

import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;

import com.telenav.snowsweeper.renderer.Util;

public class MapEdgeTouchEventHandler implements OnTouchListener, OnScaleGestureListener, OnGestureListener, OnDoubleTapListener {
	
	private View target;
	
	boolean isDragMode = false;
	private float lastDragX = -1;
	private float lastDragY = -1;
	
	private GameParamsSet gameState;
	private ScaleGestureDetector scaleDetector;
	private GestureDetector tapDetector;
	
	public MapEdgeTouchEventHandler(Context context, View target, GameParamsSet gameState) {
		this.target = target;
		this.gameState = gameState;
		target.setOnTouchListener(this);
		target.setLongClickable(true);
		scaleDetector = new ScaleGestureDetector(context, this);
		tapDetector = new GestureDetector(this);
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		Log.d("tn-debug", event.getAction() + ", " + event.getX() + ", " + event.getY());
		if (v == target) {
		    scaleDetector.onTouchEvent(event);
		    tapDetector.onTouchEvent(event);
		    if(event.getPointerCount() == 1)
		    {
			switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					lastDragX = event.getX();
					lastDragY = event.getY();
					isDragMode = true;
					break;
				case MotionEvent.ACTION_MOVE:
                    if (isDragMode)
                    {
                        float dx = (event.getX() - lastDragX) * Util.getPanSensitive();
                        float dy = (event.getY() - lastDragY) * Util.getPanSensitive();
                        gameState.move((int) -dx, (int) -dy);
                        lastDragX = event.getX();
                        lastDragY = event.getY();
                    }
					break;
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_CANCEL:
				case MotionEvent.ACTION_OUTSIDE:
					isDragMode = false;
					break;
			}
			return true;
		    }
		    else
		    {
		        isDragMode = false;
		    }
		}
		return false;
	}

    @Override
    public boolean onScale(ScaleGestureDetector detector)
    {
        float factor = detector.getScaleFactor();
        if (factor > 1)
        {
        	gameState.adjustScaleXY((int) Util.getZoomSensitive(), (int)Util.getZoomSensitive());
        } else
        {
        	gameState.adjustScaleXY(-(int)Util.getZoomSensitive(), -(int)Util.getZoomSensitive());
        }

        return false;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector)
    {
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector)
    {
    }

    @Override
    public boolean onDoubleTap(MotionEvent e)
    {
    	gameState.setScaleXY(Util.EDGE_MAX_SCALE, Util.EDGE_MAX_SCALE);
//        params.screenMove2((int)e.getX(), (int)e.getY());
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e)
    {
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e)
    {
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e)
    {
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e)
    {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        return false;
    }
}
